package com.luyang.framework.validator;

import java.util.Objects;
import java.util.function.Function;

/**
 * 捕捉 Stream 抛出受检异常
 *
 * @author lu_yang
 */
public class Try {

    public static <T, R> Function<T, R> of(UncheckedFunction<T, R> mapper) {
        Objects.requireNonNull(mapper);
        return t -> {
            try {
                return mapper.apply(t);
            } catch (Exception e) {
                throw Exceptions.unchecked(e);
            }
        };
    }

    @FunctionalInterface
    public interface UncheckedFunction<T, R> {
        /**
         * 调用
         *
         * @param t t
         * @return R
         */
        R apply(T t);
    }
}
